package TwoPointers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _18_4Sum {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            if (i == 0 || (nums[i] != nums[i - 1])) {
                for (int j = i + 1; j < nums.length; j++) {
                    if (j == i+1 || (j > 0 && nums[j] != nums[j - 1])) {
                        int left = j + 1, right = nums.length - 1;
                        while (left < right) {
                            int tempSum = nums[i] + nums[j] + nums[left] + nums[right];
                            if (tempSum == target) {
                                res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                                while (left < right && nums[left] == nums[left + 1]) {
                                    left++;
                                }
                                while (left < right && nums[right] == nums[right - 1]) {
                                    right--;
                                }
                                left++;
                                right--;
                            } else if (tempSum < target) {
                                left++;
                            } else {
                                right--;
                            }
                        }
                    }
                }
            }
        }
        return res;
    }
}
